package com.company.utils;

import org.apache.log4j.Logger;

/**
 * @Auther Xue
 * @Date 2021/4/27
 * @Version 1.0
 * @Desc    自定义异常添加log日志
 */
public class MyException extends Exception {

    static Logger logger=Logger.getLogger("MyExceptionLogger");
    public MyException(String message) {
        super(message);
        logger.error(message);
    }
    public static String getExceptionMessageToString(Exception e){
        String retStr="";
        retStr=retStr+e.getClass().getName()+":"+e.getMessage()+"\n";
        StackTraceElement[] elements=  e.getStackTrace();  //获取异常跟踪信息
        for (StackTraceElement element : elements) {
            retStr=retStr+"    at "+element.getClassName()+"."+element.getMethodName()+
                    "("+element.getFileName()+":"+element.getLineNumber()+")"+"\n";
        }
        return retStr;
    }
    public static void logExceptionMessage(Exception e){
        String retStr="";
        retStr=retStr+e.getClass().getName()+":"+e.getMessage()+"\n";
        StackTraceElement[] elements=  e.getStackTrace();  //获取异常跟踪信息
        for (StackTraceElement element : elements) {
            retStr=retStr+"    at "+element.getClassName()+"."+element.getMethodName()+
                    "("+element.getFileName()+":"+element.getLineNumber()+")"+"\n";
        }
        logger.error(retStr);
    }
}
